| Conditions | 1 |
| Paths | 1 |
| Total Lines | 118 |
| Code Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 20 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var TableHelper = { |
||
| 251 | var TableSingleton = (function(){ |
||
| 252 | // Instance stores a reference to the Singleton |
||
| 253 | var instance; |
||
| 254 | function initInstance(){ |
||
| 255 | // Singleton |
||
| 256 | // Private methods and variables |
||
| 257 | function BuildRequest(rq, crntTableId, strDesc){ |
||
| 258 | rq.tableId = crntTableId; |
||
| 259 | TableHelper.BuildRequest.Sort(rq, strDesc); |
||
| 260 | TableHelper.BuildRequest.Filter(rq); |
||
| 261 | }; |
||
| 262 | var tail = null; |
||
| 263 | function LoadData(tableContainer, rq){ |
||
| 264 | if(tail!==null){ tail.abort();} |
||
| 265 | TableHelper.LoadData.SetVisability(tableContainer, false); |
||
| 266 | var xmlhttp = window.XMLHttpRequest ? |
||
| 267 | new XMLHttpRequest() : /* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
| 268 | new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
| 269 | xmlhttp.onreadystatechange = function(){ |
||
| 270 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
| 271 | var d = JSON.parse(xmlhttp.responseText); |
||
| 272 | TableHelper.Draw(tableContainer, d, instance); |
||
| 273 | TableHelper.LoadData.SetVisability(tableContainer, true); |
||
| 274 | instance.LoadEndCalback(tableContainer); |
||
| 275 | } |
||
| 276 | }; |
||
| 277 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
| 278 | xmlhttp.send(); |
||
| 279 | tail = xmlhttp; //put at tail to can abort later any previous |
||
| 280 | } |
||
| 281 | function ReloadData(tableId){ |
||
| 282 | var request = {}; |
||
| 283 | BuildRequest(request, tableId, this.strDesc); |
||
| 284 | LoadData(tableId, request); |
||
| 285 | } |
||
| 286 | |||
| 287 | var GoPageGetNo = TableHelper.GoPage.GetNo; |
||
| 288 | var getParent = TableHelper.GetParent; |
||
| 289 | var RequestToUrl = TableHelper.RequestToUrl; |
||
| 290 | |||
| 291 | return { |
||
| 292 | rq: null, |
||
| 293 | strAsc: String.fromCharCode(9650), //▲ |
||
| 294 | strDesc: String.fromCharCode(9660),//▼ |
||
| 295 | ColumnHover: TableHelper.ColumnHover, //function(tableContainer, index) |
||
| 296 | Export: function(lnk, eType){ |
||
| 297 | var request = {}; |
||
| 298 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 299 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 300 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
| 301 | eType : |
||
| 302 | "csv"; |
||
| 303 | window.open(RequestToUrl(request)); |
||
| 304 | return false; |
||
| 305 | }, |
||
| 306 | Filter: function(field){ |
||
| 307 | var crntTableId = TableHelper.Filter.GetTableId(field); |
||
| 308 | if(crntTableId !== null){ |
||
| 309 | var request = {}; |
||
| 310 | var exRq = this.rq; |
||
| 311 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 312 | if(exRq === null || |
||
| 313 | request.filter !== exRq.filter || |
||
| 314 | request.filterBy !== exRq.filterBy |
||
| 315 | ){ |
||
| 316 | LoadData(crntTableId, request); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | }, |
||
| 320 | GoPage: function(lnk){ |
||
| 321 | var request = {}; |
||
| 322 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 323 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 324 | request.pageNo = GoPageGetNo(lnk, crntTableId); |
||
| 325 | LoadData(crntTableId, request); |
||
| 326 | return false; |
||
| 327 | }, |
||
| 328 | init: function (tableId){ |
||
| 329 | var tContainer = document.getElementById(tableId); |
||
| 330 | if(!TableHelper.IePrior(9)){ |
||
| 331 | TableHelper.Init.SetColumnsHoverEffect(tContainer, tableId); |
||
| 332 | } |
||
| 333 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
| 334 | TableHelper.ProcessPaginationLinks(tfoot); |
||
| 335 | }, |
||
| 336 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
| 337 | ReloadData: ReloadData, |
||
| 338 | Sort: function(colNo, lnk){ |
||
| 339 | var request = {}; |
||
| 340 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 341 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 342 | if(Math.round(colNo) === request.colNo){ |
||
| 343 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
| 344 | }else{ |
||
| 345 | request.colNo = Math.round(colNo); |
||
| 346 | request.colOrd = "asc"; |
||
| 347 | } |
||
| 348 | LoadData(crntTableId, request); |
||
| 349 | /* Clear and add new sort arrow */ |
||
| 350 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
| 351 | var length = headSpans.length; |
||
| 352 | for(var i = 0; i < length; i++){ |
||
| 353 | headSpans[i].innerHTML = ""; |
||
| 354 | } |
||
| 355 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
| 356 | } |
||
| 357 | }; |
||
| 358 | } |
||
| 359 | return { |
||
| 360 | //Get the Singleton instance if one exists, or create one if it doesn't |
||
| 361 | getInstance: function(){ |
||
| 362 | if(!instance){ |
||
| 363 | instance = initInstance(); |
||
| 364 | } |
||
| 365 | return instance; |
||
| 366 | } |
||
| 367 | }; |
||
| 368 | })(); |
||
| 369 | var table = TableSingleton.getInstance(); |
||
| 370 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: